﻿using System;
using System.Collections.Generic;
using BlueDot.Data.Core;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Syncronization.Utils;

namespace Syncronization
{
    public class mfLYSyncAgent : SyncAgent
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="mfLYSyncAgent"/> class.
        /// </summary>
        /// <param name="remoteProvider">The remote provider.</param>
        /// <param name="clientSyncProvider">The client sync provider.</param>
        public mfLYSyncAgent(ServerSyncProvider remoteProvider, SyncProvider clientSyncProvider)
        {
            LocalProvider = clientSyncProvider;
            RemoteProvider = remoteProvider;            
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the type of the tombstone base.
        /// </summary>
        /// <value>The type of the tombstone base.</value>
        public Type TombstoneBaseType { get; set; }

        #endregion

        #region public

        /// <summary>
        /// Adds the schema.
        /// </summary>
        /// <param name="schemaBuilders">The schema builders.</param>
        /// <param name="syncDirection">The sync direction.</param>
        /// <param name="tableCreationOption">The table creation option.</param>
        public void AddSchema(IDictionary<string, ISchemaBuilder> schemaBuilders, SyncDirection syncDirection,
                              TableCreationOption tableCreationOption)
        {
            AddSchema(schemaBuilders, syncDirection, tableCreationOption, string.Empty);
        }

        /// <summary>
        /// Adds the schema.
        /// </summary>
        /// <param name="schemaBuilders">The schema builders.</param>
        /// <param name="syncDirection">The sync direction.</param>
        /// <param name="tableCreationOption">The table creation option.</param>
        /// <param name="groupName">Name of the group.</param>
        public void AddSchema(IDictionary<string, ISchemaBuilder> schemaBuilders, SyncDirection syncDirection,
                              TableCreationOption tableCreationOption, string groupName)
        {
            schemaBuilders.Each(item => AddSchema(item.Value, syncDirection, tableCreationOption, groupName));
        }

        /// <summary>
        /// Adds the entity.
        /// </summary>
        /// <param name="schemaBuilder">The schema builder.</param>
        /// <param name="syncDirection">The sync direction.</param>
        /// <param name="tableCreationOption">The table creation option.</param>
        /// <param name="groupName">Name of the group.</param>
        public void AddSchema(ISchemaBuilder schemaBuilder, SyncDirection syncDirection,
                              TableCreationOption tableCreationOption, string groupName)
        {
            Guard.ArgumentNotNull(schemaBuilder, "schemaBuilder");
            Guard.ArgumentNotNullOrEmptyString(groupName, "groupName");

            var entityBuilder = schemaBuilder as EntityBuilder;
            if (entityBuilder != null)
            {
                if (TombstoneBaseType != null && entityBuilder.BaseEntityType == TombstoneBaseType)
                    return;
            }

            if (Configuration.SyncTables.Contains(schemaBuilder.Name))
                return;

            SyncTable syncTable = Configuration.SyncTables.Add(schemaBuilder.Name, tableCreationOption, syncDirection);

            if (!string.IsNullOrEmpty(groupName))
                syncTable.SyncGroup = new SyncGroup {GroupName = groupName};

            var serverSyncProvider = RemoteProvider as IServerSyncProvider;
            if (serverSyncProvider == null)
                return;

            serverSyncProvider.AddSchema(schemaBuilder);
        }

        #endregion
    }
}